package com.codedef.diaryserver.service;

import com.codedef.diaryserver.domain.DiaryDao;
import com.codedef.diaryserver.domain.UserDao;
import com.codedef.diaryserver.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private HttpSession session;

    public void addUser(UserDao userInfo) {
        UserDao userDao = new UserDao();
        userDao.setUsername(userInfo.getUsername());
        userRepository.save(userDao);
    }

    public boolean userLogin(String username, String password) {
        UserDao user = userRepository.findUserDaoByUsername(username);
        if (user.getPassword().equals(password))
            return true;
        return false;
    }

    public UserDao getUserInfo() {
        Integer id = getSessionUserId();
        return userRepository.findById(id).get();
    }

    public Integer getUserId(String username) {
        return userRepository.findUserDaoByUsername(username).getId();
    }

    public Integer getSessionUserId() {
        return (Integer) session.getAttribute("USER_ID");
    }

    public String getSessionUsername() {
        return (String) session.getAttribute("USER_USERNAME");
    }

}
